package com.dinner.model;

import java.sql.Connection;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class MenuTypeDaoHibernate extends HibernateDaoSupport implements
		MenuTypeDao {

	@Override public MenuType getMenuTypeByID(int typeID) {
		SessionFactory sessionFactory = this.getSessionFactory();
		Session session = sessionFactory.openSession();
		int isolation = -1;
		try {
			isolation = session.connection().getTransactionIsolation();
			session.connection().setTransactionIsolation(
					Connection.TRANSACTION_SERIALIZABLE);
			session.beginTransaction();
			MenuType type = ((MenuType) getHibernateTemplate().get(
					MenuType.class, typeID));
			session.getTransaction().commit();
			session.connection().setTransactionIsolation(isolation);
			return type;

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			session.close();
		}
	}

	private void saveMenuType(MenuType menuType) {
		this.getHibernateTemplate().save(menuType);
	}

	@SuppressWarnings("unchecked")
	@Override public List<MenuType> getMenuTypesBySeller(String sellerID) {
		SessionFactory sessionFactory = this.getSessionFactory();
		Session session = sessionFactory.openSession();
		int isolation = -1;
		try {
			isolation = session.connection().getTransactionIsolation();
			session.connection().setTransactionIsolation(
					Connection.TRANSACTION_SERIALIZABLE);
			session.beginTransaction();
			List<MenuType> list = (java.util.List<MenuType>) getHibernateTemplate()
					.find(
							"FROM com.dinner.model.MenuType type where "
									+ "type.sellerID = ?",
							new String[] { sellerID });
			session.getTransaction().commit();
			session.connection().setTransactionIsolation(isolation);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			Transaction tx = session.getTransaction();
			if (tx.isActive())
				tx.rollback();
			return java.util.Collections.EMPTY_LIST;
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override public boolean addMenuType(MenuType menuType) {
		boolean access = false;
		SessionFactory sessionFactory = this.getSessionFactory();
		Session session = sessionFactory.openSession();
		int isolation = -1;
		try {
			isolation = session.connection().getTransactionIsolation();
			session.connection().setTransactionIsolation(
					Connection.TRANSACTION_SERIALIZABLE);
			session.beginTransaction();
			List<MenuType> list = (java.util.List<MenuType>) getHibernateTemplate()
					.find(
							"FROM com.dinner.model.MenuType type where type.description = ? and "
									+ "type.sellerID = ?",
							new String[] { menuType.getDescription(),
									menuType.getSellerID() });
			if (list.isEmpty()) {
				saveMenuType(menuType);
				access = true;
			}
			session.getTransaction().commit();
			session.connection().setTransactionIsolation(isolation);
			return access;
		} catch (Exception e) {
			e.printStackTrace();
			Transaction tx = session.getTransaction();
			if (tx.isActive())
				tx.rollback();
			return false;
		} finally {
			session.close();
		}
	}

	@Override public void removeMenuType(MenuType menuType) {
		SessionFactory sessionFactory = this.getSessionFactory();
		Session session = sessionFactory.openSession();
		int isolation = -1;
		try {
			isolation = session.connection().getTransactionIsolation();
			session.connection().setTransactionIsolation(
					Connection.TRANSACTION_SERIALIZABLE);
			session.beginTransaction();
			getHibernateTemplate().delete(menuType);
			session.getTransaction().commit();
			session.connection().setTransactionIsolation(isolation);
		} catch (Exception e) {
			e.printStackTrace();
			Transaction tx = session.getTransaction();
			if (tx.isActive())
				tx.rollback();
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override public boolean updateMenuType(MenuType menuType) {
		boolean access = false;
		SessionFactory sessionFactory = this.getSessionFactory();
		Session session = sessionFactory.openSession();
		int isolation = -1;
		try {
			isolation = session.connection().getTransactionIsolation();
			session.connection().setTransactionIsolation(
					Connection.TRANSACTION_SERIALIZABLE);
			session.beginTransaction();
			List<MenuType> list = (java.util.List<MenuType>) getHibernateTemplate()
					.find(
							"FROM com.dinner.model.MenuType type where type.description = ? and "
									+ "type.sellerID = ?",
							new String[] { menuType.getDescription(),
									menuType.getSellerID() });
			if (list.isEmpty()) {
				this.getHibernateTemplate().update(menuType);
				access = true;
			}
			session.getTransaction().commit();
			session.connection().setTransactionIsolation(isolation);
			return access;
		} catch (Exception e) {
			e.printStackTrace();
			Transaction tx = session.getTransaction();
			if (tx.isActive())
				tx.rollback();
			return false;
		} finally {
			session.close();
		}
	}

}
